package org.tacticalTroopers.jme.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;
import org.tacticalTroopers.jme.common.message.GameMap;
import org.tacticalTroopers.jme.common.message.GameState;
import org.tacticalTroopers.jme.common.message.Id;
import org.tacticalTroopers.jme.common.message.Information;
import org.tacticalTroopers.jme.common.message.PlayerNames;
import org.tacticalTroopers.jme.common.message.SerializationInitializer;

import com.jme3.network.connection.Client;
import com.jme3.network.events.MessageListener;
import com.jme3.network.message.Message;

public class TestClient {

	Log log = LogFactory.getLog(getClass());

	String playerName;

	public TestClient(){

		PropertyConfigurator.configure("log4j.properties");
		log.info("[CLIENT]");
		initConfiguration();
		SerializationInitializer.init();
		final Client c= new Client();
		try {
			c.connect("localhost",56650,56651);
		} catch (IllegalArgumentException e) {
			log.error(e);
		} catch (IOException e) {
			log.error(e);
		}


		c.addMessageListener(new MessageListener() {

			@Override
			public void objectSent(Object arg0) {}

			@Override
			public void objectReceived(Object arg0) {}

			@Override
			public void messageSent(Message arg0) {}

			@Override
			public void messageReceived(Message message) {
				log.info("Received Id:"+((Id)message).getValue());
				try {
					c.send(new Information().setKey("playerName").setValue(playerName));
				} catch (IOException e) {
					
				}
			}
		},Id.class);

		c.addMessageListener(new MessageListener() {

			@Override
			public void objectSent(Object arg0) {}

			@Override
			public void objectReceived(Object arg0) {}

			@Override
			public void messageSent(Message arg0) {}

			@Override
			public void messageReceived(Message message) {
				log.info("Received GameMap:"+((GameMap)message));
			}
		},GameMap.class);

		c.addMessageListener(new MessageListener() {

			@Override
			public void objectSent(Object arg0) {}

			@Override
			public void objectReceived(Object arg0) {}

			@Override
			public void messageSent(Message arg0) {}

			@Override
			public void messageReceived(Message message) {
				log.debug("Received GameState:"+((GameState)message));
			}
		},GameState.class);
		
		c.addMessageListener(new MessageListener() {

			@Override
			public void objectSent(Object arg0) {}

			@Override
			public void objectReceived(Object arg0) {}

			@Override
			public void messageSent(Message arg0) {}

			@Override
			public void messageReceived(Message message) {
				log.debug("Received PlayerNames:"+((PlayerNames)message));
			}
		},PlayerNames.class);

		c.start();


	}
	
	public void initConfiguration() {

		File conf = new File("tacticalTrooperTest.cfg");
		if(!conf.exists()){
			FileWriter fw;
			try {
				fw = new FileWriter(conf);
				fw.append("playerName=TestTrooper");
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		Scanner scanner=null;
		try {
			scanner = new Scanner(new FileInputStream(conf));
			while (scanner.hasNextLine()){
				playerName=scanner.nextLine().substring(11);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			scanner.close();
		}

	}


	public static void main(String[] args) {
		new TestClient();
	}

}
